Imports System.IO
Imports System.Windows.Forms
Imports DemosCommonCode.Imaging.Codecs.Dialogs
Imports Vintasoft.Imaging.Codecs

Namespace Imaging.Codecs
	Public Class EncoderFactory

		#Region "Properties"

		Shared _dialogsStartPosition As FormStartPosition = FormStartPosition.CenterParent
		Public Shared Property DialogsStartPosition() As FormStartPosition
			Get
				Return _dialogsStartPosition
			End Get
			Set
				_dialogsStartPosition = value
			End Set
		End Property

		#End Region



		#Region "Methods"

		Public Shared Function GetEncoder(filename As String, ByRef encoder As EncoderBase) As Boolean
			Return GetEncoder(filename, File.Exists(filename), True, encoder)
		End Function

		Public Shared Function GetEncoder(filename As String, ByRef encoder As EncoderBase, checkFileOverwrite As Boolean) As Boolean
			Return GetEncoder(filename, File.Exists(filename), checkFileOverwrite, encoder)
		End Function

		Public Shared Function GetEncoder(filename As String, appendExistingDocumentEnabled As Boolean, checkFileOverwrite As Boolean, ByRef encoder As EncoderBase) As Boolean
			encoder = AvailableEncoders.CreateEncoder(filename)
			Dim result As Boolean = True
			If encoder IsNot Nothing Then
				result = SetEncoderSettings(encoder, appendExistingDocumentEnabled)
				If result AndAlso checkFileOverwrite AndAlso Not CheckOverwriteFile(encoder, filename) Then
					Return False
				End If
			End If
			Return result
		End Function

		Public Shared Function GetMultipageEncoder(filename As String, ByRef encoder As MultipageEncoderBase) As Boolean
			Return GetMultipageEncoder(filename, File.Exists(filename), True, encoder)
		End Function

		Public Shared Function GetMultipageEncoder(filename As String, ByRef encoder As MultipageEncoderBase, checkFileOverwrite As Boolean) As Boolean
			Return GetMultipageEncoder(filename, File.Exists(filename), checkFileOverwrite, encoder)
		End Function

		Public Shared Function GetMultipageEncoder(filename As String, appendExistingDocumentEnabled As Boolean, checkFileOverwrite As Boolean, ByRef encoder As MultipageEncoderBase) As Boolean
			encoder = TryCast(AvailableEncoders.CreateEncoder(filename), MultipageEncoderBase)
			Dim result As Boolean = True
			If encoder IsNot Nothing Then
				result = SetEncoderSettings(encoder, appendExistingDocumentEnabled)
				If result AndAlso checkFileOverwrite AndAlso encoder.CreateNewFile AndAlso Not CheckOverwriteFile(encoder, filename) Then
					Return False
				End If
			End If
			Return result
		End Function

		Public Shared Function SetEncoderSettings(encoder As EncoderBase, appendExistingDocumentEnabled As Boolean) As Boolean
			' set encoder settings
			Select Case encoder.Name
				Case "Png"
					Dim pngEncoderSettingsForm As New PngEncoderSettingsForm()
					pngEncoderSettingsForm.StartPosition = DialogsStartPosition
					If pngEncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, PngEncoder).Settings = pngEncoderSettingsForm.EncoderSettings
					Exit Select

				Case "Gif"
					Dim gifEncoderSettingsForm As New GifEncoderSettingsForm()
					gifEncoderSettingsForm.StartPosition = DialogsStartPosition
					gifEncoderSettingsForm.AppendExistingDocumentEnabled = appendExistingDocumentEnabled
					If gifEncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, GifEncoder).Settings = gifEncoderSettingsForm.EncoderSettings
					DirectCast(encoder, MultipageEncoderBase).CreateNewFile = Not gifEncoderSettingsForm.AppendExistingDocument
					Exit Select

				Case "Jpeg"
					Dim jpegEncoderSettingsForm As New JpegEncoderSettingsForm()
					jpegEncoderSettingsForm.StartPosition = DialogsStartPosition
					If jpegEncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, JpegEncoder).Settings = jpegEncoderSettingsForm.EncoderSettings
					Exit Select

				Case "Jpeg2000"
					Dim jpeg2000EncoderSettingsForm As New Jpeg2000EncoderSettingsForm()
					jpeg2000EncoderSettingsForm.StartPosition = DialogsStartPosition
					If jpeg2000EncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, IJpeg2000Encoder).Settings = jpeg2000EncoderSettingsForm.EncoderSettings
					Exit Select

				Case "Pdf"
					Dim pdfEncoderSettingsForm As New PdfEncoderSettingsForm()
					pdfEncoderSettingsForm.StartPosition = DialogsStartPosition
					pdfEncoderSettingsForm.AppendExistingDocumentEnabled = appendExistingDocumentEnabled
					If pdfEncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, IPdfEncoder).Settings = pdfEncoderSettingsForm.EncoderSettings
					DirectCast(encoder, MultipageEncoderBase).CreateNewFile = Not pdfEncoderSettingsForm.AppendExistingDocument
					Exit Select

				Case "Tiff"
					Dim tiffEncoderSettingsForm As New TiffEncoderSettingsForm()
					tiffEncoderSettingsForm.StartPosition = DialogsStartPosition
					tiffEncoderSettingsForm.AppendExistingDocumentEnabled = appendExistingDocumentEnabled
					If tiffEncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, TiffEncoder).Settings = tiffEncoderSettingsForm.EncoderSettings
					DirectCast(encoder, MultipageEncoderBase).CreateNewFile = Not tiffEncoderSettingsForm.AppendExistingDocument
					Exit Select

				Case "Jbig2"
					Dim jbig2EncoderSettingsForm As New Jbig2EncoderSettingsForm()
					jbig2EncoderSettingsForm.StartPosition = DialogsStartPosition
					jbig2EncoderSettingsForm.AppendExistingDocumentEnabled = appendExistingDocumentEnabled
					If jbig2EncoderSettingsForm.ShowDialog() <> DialogResult.OK Then
						Return False
					End If
					DirectCast(encoder, IJbig2Encoder).Settings = jbig2EncoderSettingsForm.EncoderSettings
					DirectCast(encoder, MultipageEncoderBase).CreateNewFile = Not jbig2EncoderSettingsForm.AppendExistingDocument
					Exit Select
			End Select
			Return True
		End Function

		Private Shared Function CheckOverwriteFile(encoder As EncoderBase, filename As String) As Boolean
			If File.Exists(filename) Then
				Dim isMultipageEncoder As Boolean = TypeOf encoder Is MultipageEncoderBase
				If (isMultipageEncoder AndAlso DirectCast(encoder, MultipageEncoderBase).CreateNewFile) OrElse Not isMultipageEncoder Then
					Dim message As String = String.Format("File '{0}' already exists. Overwrite it?", Path.GetFileName(filename))
					If MessageBox.Show(message, "Confirm overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
						Return False
					End If
				End If
			End If
			Return True
		End Function

		#End Region

	End Class
End Namespace
